// pages/bumpbasic/bumpbasic.js

var util = require('../../utils/MD5.js');
var myjson = require('../../utils/JSON.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bumStationCheck: "0",
     bumpChange: "0",
     bumpCount: "0",
    bumpRestPeriod: "0", 
    bumpRunTime: "0", 
    bumpStationCheckPeriod: "0", 
    bumpStationCheckTime: "0",
    bumpStationFlows: "0", 
    bumpStationRunTime: "0",
    bumpSuspendTime: "0",
    electricityRange: "0",
    startWaterLevel: "0",   
    stopWaterLevel: "0",
    warningWaterLevel: "0", 
    waterLevelRange: "0", 
    grillageRestPeriod: "0",
    maxOverLoadDelayTime: "0",
    maxOverLoadIndex: "0",
    settingElectricity: "0",
    autoRollbacktimes:"0",
    bumpRunTime: "",
    bumpSuspendTime: ""
  },

  clearData: function (e) {
    //获得表单数据
    var that = this

    let map = new Map();
    var $id = e.currentTarget.id;
    if ($id == 95) {
      map.set('bumpStationFlowsClear', '1');
    } else if ($id == 96) {
      map.set('bumpStationRunTimeClear', '1');
    } else if ($id == 97) {
      map.set('electricityRangeClear', '1');
    } else if ($id == 98) {
      map.set('waterLevelRangeClear', '1');
    } 

    var json = myjson.mapToJson(map);
    console.log('json:' + json + " id:" + $id);
    var sid = wx.getStorageSync('sid');
    var timestamp = Date.parse(new Date());

    timestamp = timestamp / 1000;
    var token = timestamp + "@13f@" + sid
    token = util.hexMD5(token);
    var id = wx.getStorageSync('id');

    wx.request({
      url: "https://www.gzcri.cn/api/bump/edit.d",
      data: {
        code: timestamp,
        token: token,
        data: json,
        sid: sid,
        index: id
      },
      header: { "content-type": "application/x-www-form-urlencoded" },
      method: "POST",
      success: function (res) {
        var responseData = res.data;
        console.log(responseData.result);
        if (res.data.status == -1) {
          console.log(responseData.result);
        } else {
          console.log('success');
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.showLoading({
      title: '数据获取中....',
    })

    var that = this
    //获取本地数据
    wx.getStorage({
      key: 'bumpdata',
      success: function (res) {
        console.log('getstoreage bumpdata:' + res.data);
        var fixworkingWaterLevel = res.data.stationInfo.workingWaterLevel * 1;
        var fixwarningWaterLevel = res.data.stationInfo.warningWaterLevel * 1;
        var fixstopWaterLevel = res.data.stationInfo.stopWaterLevel * 1;

        var fixbumStationCheck = res.data.stationInfo.bumStationCheck * 1;
        var fixbumpChange = res.data.stationInfo.bumpChange * 1;
        var fixbumpCount = res.data.stationInfo.bumpCount * 1;
        var fixbumpRestPeriod = res.data.stationInfo.bumpRestPeriod * 1;
        // var fixbumpRunTime: "",
        var fixbumpStationCheckPeriod = res.data.stationInfo.bumpStationCheckPeriod * 1;
        var fixbumpStationCheckTime = res.data.stationInfo.bumpStationCheckTime  * 1;
        var fixbumpStationFlows = res.data.stationInfo.bumpStationFlows  * 1;
        var fixbumpStationRunTime = res.data.stationInfo.bumpStationRunTime  * 1;
        // var fixbumpSuspendTime: "",
        var fixelectricityRange = res.data.stationInfo.electricityRange  * 1;
        // var fixstartWaterLevel: "",
        var fixwaterLevelRange = res.data.stationInfo.waterLevelRange  * 1;
        var fixworkingWaterLeve = res.data.stationInfo.workingWaterLeve * 1;
        var fixgrillageRestPeriod = res.data.stationInfo.grillageRestPeriod * 1;
        var fixmaxOverLoadDelayTime = res.data.stationInfo.grillageRestPeriod * 1;
        var fixmaxOverLoadIndex = res.data.stationInfo.maxOverLoadIndex * 1;
        var fixsettingElectricity = res.data.stationInfo.settingElectricity * 1;
        var fixautoRollbacktimes = res.data.stationInfo.autoRollbacktimes  * 1;
        var fixstartWaterLevel = res.data.stationInfo.startWaterLevel * 1; 
        that.setData({
          'bumpRunTime': res.data.stationInfo.bumpRunTime,
          'bumpSuspendTime': res.data.stationInfo.bumpSuspendTime,
          'workingWaterLevel': fixworkingWaterLevel.toFixed(2),
          'warningWaterLevel': fixwarningWaterLevel.toFixed(2),
          'startWaterLevel': fixstartWaterLevel.toFixed(2),
          'stopWaterLevel': fixstopWaterLevel.toFixed(2),
          'bumStationCheck': fixbumStationCheck,
          'bumpChange': fixbumpChange,
          'bumpCount': fixbumpCount,
          'bumpRestPeriod': fixbumpRestPeriod,
          'bumpStationCheckPeriod': fixbumpStationCheckPeriod,
          'bumpStationCheckTime': fixbumpStationCheckTime,
          'bumpStationFlows': fixbumpStationFlows.toFixed(2),
          'bumpStationRunTime': fixbumpStationRunTime,
          'electricityRange': fixelectricityRange,
          'waterLevelRange': fixwaterLevelRange,
          'grillageRestPeriod': fixgrillageRestPeriod,
          'maxOverLoadDelayTime': fixmaxOverLoadDelayTime,
          'maxOverLoadIndex': fixmaxOverLoadIndex,
          'settingElectricity': fixsettingElectricity,
          'autoRollbacktimes': fixautoRollbacktimes,
          'bumpRunTime': res.data.stationInfo.bumpRunTime,
          'bumpSuspendTime': res.data.stationInfo.bumpSuspendTime,
        });

        wx.hideLoading();
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})